package com.kuang.controller;

import com.kuang.pojo.Commodity;
import com.kuang.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @description: 商品控制器
 * @author: wuhaohua
 * @date: Created in 2022/4/10 20:33
 * @version: 1.0
 */
@RestController
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @GetMapping("/parse/{keyword}")
    public Boolean parse(@PathVariable("keyword") String keyword) {
        return commodityService.parseCommodity(keyword);
    }

    @GetMapping("/search/{keyword}/{pageNum}/{pageSize}")
    public List<Commodity> search(@PathVariable("keyword") String keyword,
                                  @PathVariable("pageNum") int pageNum,
                                  @PathVariable("pageSize") int pageSize) throws IOException {
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        // 抓取数据
        commodityService.parseCommodity(keyword);
        //查询数据
        return commodityService.searchPage(keyword, pageNum, pageSize);
    }

    @GetMapping("/searchHighLight/{keyword}/{pageNum}/{pageSize}")
    public List<Commodity> searchHighLight(@PathVariable("keyword") String keyword,
                                  @PathVariable("pageNum") int pageNum,
                                  @PathVariable("pageSize") int pageSize) throws IOException {
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }

        // 抓取数据
        commodityService.parseCommodity(keyword);
        //查询数据
        return commodityService.searchHighLightPage(keyword, pageNum, pageSize);
    }

}
